<?php

namespace Common\Logic;

/**
 * 钥匙包客栈逻辑
 * @modify wenhui 2016-12-28 创建
 */
class HotelFindLogic extends \Common\Controller\BaseController {

    /**
     * 获取客栈逻辑
     * @modify wenhui 2016-12-28 创建
     * @modify wenhui 2017-02-22 返回市所在省的客栈 v1.0.02版本判断
     * @param  integer   $city     城市id
     * @param  integer   $longtitude 经纬度
     * @param  integer   $latitude    经纬度
     * @param  integer   $pageSize     每页记录
     * @param  integer   $pageIndex     页码
     * @param  integer   $version     版本号
     */
    public function getHotelNear($city, $longtitude, $latitude, $pageSize, $pageIndex, $version) {
        // 获取客栈
        $hotelEntityModel = D('HotelEntity');
        $hotelDetailModel = D('HotelDetail');
        $cityModel = D('City');
        $hotelSpecialModel = D('HotelSpecial');

        $testHotel = array();
        if (!is_null($city) && !is_null($longtitude) && !is_null($latitude)) {

            if ($version >= '1.0.02') {
              // 获取城市 所在的省微预定客栈数据
              $cityInfo = $cityModel->getInfo($city);
              $provinceId = $cityInfo[0]['province_id'];
              $provinceInfo = $cityModel->getInfoByProvince($provinceId);
              $city = array_column($provinceInfo, 'id');
            }

            $hotelResult = $hotelEntityModel->getInfoByCity($city);
            $hotelResult = getArrayValueAsIndex($hotelResult, 'id');
            $commit = true;
            if (empty($hotelResult)) {
                $commit = false;
            } else {
                $hotelIds = array_column($hotelResult , 'id');
                // 根据客栈ids 获取客栈详情      
                $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
                if (empty($hotelinfo)) {
                    $commit = false;
                }
            }

            if ($commit) {
                foreach ($hotelinfo as $key => &$hotelList) {
                    $hotelSpecialDetail = $hotelSpecialModel->getInfoByHotel($hotelList['hotelentity_id']);
                    // 过滤没有名称的酒店 和 没有封面 和 过滤没有掌柜说的客栈
                    if (empty($hotelList['name']) || empty($hotelList['coverimageurl'])  || empty($hotelList['description']) || empty($hotelSpecialDetail)) {
                        unset($hotelinfo[$key]);
                        continue;
                    }

                    // 计算用户经纬度与客栈经纬度 两点的距离
                    if (!empty($hotelList['longtitude']) && $hotelList['latitude'] != 0.000000) {
                        $distance = getDistance($latitude, $longtitude, $hotelList['latitude'], $hotelList['longtitude']);
                        $hotelList['distance'] = $distance;
                    } else {
                        $hotelList['distance'] = 99999999;
                    }
                    // 标志不是推荐数据
                    $hotelList['isRecommend'] = DISABLE;
                    // 处理数据
                    $hotelList['isEnable'] = 0;
                    $hotelList['hotel'] = $hotelList['hotelentity_id'];
                    $hotelList['coverUrl'] =  MEIZHU_URL . $hotelList['coverimageurl'];

                    unset($hotelList['hotelentity_id']);
                    unset($hotelList['coverimageurl']);
                    unset($hotelList['special']);
                    unset($hotelList['specialimageurl']);
                    unset($hotelList['telephone']);
                    unset($hotelList['wechat']);
                    unset($hotelList['hotline']);
                    unset($hotelList['mobile']);
                    unset($hotelList['contact']);
                    unset($hotelList['ordertimeout']);
                    unset($hotelList['bookmode']);
                    unset($hotelList['enable']);
                    unset($hotelList['special']);

                    // 筛选测试客栈 usage = 3
                    if ($hotelResult[$hotelList['hotel']]['usage'] == 3) {
                        array_push($testHotel, $hotelList);
                        unset($hotelinfo[$key]);
                    }
                }
                unset($hotelList);

                // 排序
                usort($hotelinfo, function($left, $right) {
                    return $left['distance'] - $right['distance'];
                });
                // 测试客栈排序到最后
                $hotelinfo = array_merge($hotelinfo, $testHotel);
                if (!empty($hotelinfo)) {
                  // 分页
                  $hotelResult = generatePageInfo($hotelinfo, $pageIndex, $pageSize);
                  return $hotelResult;
                }
            }
        }

        // 城市没客栈 或 客栈没开通微预定 就按已经开通微预定客栈的点击量排序 
        // 已经禁用的客栈不获取
        $allHotelResult = $hotelEntityModel->getInfoAllCity();
        $allHotelResult = getArrayValueAsIndex($allHotelResult, 'id');
        if (empty($allHotelResult)) {
            return false;
        }

        $allHotelIds = array_column($allHotelResult , 'id');
        // 根据客栈ids 获取客栈详情      
        $allHotelinfo = $hotelDetailModel->getInfoByHotels($allHotelIds);
        if (empty($allHotelinfo)) {
            return false;
        }

        // 过滤没有名称的酒店 和 没有封面 和 美住线上测试客栈排序到最后
        foreach ($allHotelinfo as $key => &$allHotelList) {
            $hotelSpecialDetail = $hotelSpecialModel->getInfoByHotel($allHotelList['hotelentity_id']);
            if (empty($allHotelList['name']) || empty($allHotelList['coverimageurl'])  || empty($allHotelList['description']) || empty($hotelSpecialDetail)) {
                unset($allHotelinfo[$key]);
                continue;
            }

            // 作为推荐
            $allHotelList['isRecommend'] = ENABLE;
            // 处理数据
            $allHotelList['coverUrl'] =  MEIZHU_URL . $allHotelList['coverimageurl'];
            $allHotelList['hotel'] = $allHotelList['hotelentity_id'];
            $allHotelList['isEnable'] = 0;

            unset($allHotelList['hotelentity_id']);
            unset($allHotelList['coverimageurl']);
            unset($allHotelList['special']);
            unset($allHotelList['specialimageurl']);
            unset($allHotelList['telephone']);
            unset($allHotelList['wechat']);
            unset($allHotelList['hotline']);
            unset($allHotelList['mobile']);
            unset($allHotelList['contact']);
            unset($allHotelList['ordertimeout']);
            unset($allHotelList['bookmode']);
            unset($allHotelList['enable']);
            unset($allHotelList['special']);

            // 筛选测试客栈 usage = 3
            if ($allHotelResult[$allHotelList['hotel']]['usage'] == 3) {
                array_push($testHotel, $allHotelList);
                unset($allHotelinfo[$key]);
            }
        }
        unset($allHotelList);

        // 按点击量排序 
        usort($allHotelinfo, function($left, $right) {
            return $right['click'] - $left['click'];
        });
        //美住线上测试客栈排序到最后
        $allHotelinfo = array_merge($allHotelinfo, $testHotel);
        // 分页
        $allHotelResult = generatePageInfo($allHotelinfo, $pageIndex, $pageSize);

        return $allHotelResult;
    }

    /**
     * 实时搜索客栈
     * @modify wenhui 2016-12-28 创建
     * @param  integer   $cityId     城市id
     * @param  string   $keyWord     Keyword关键字
     */
    public function searchHotel($city, $keyWord) {
        // 获取客栈
        $hotelEntityModel = D('HotelEntity');
        $hotelDetailModel = D('HotelDetail');

        $searchHotelList = array();
        if (is_null($city) && is_null($keyWord)) {
            $hotelDetail = $hotelDetailModel->searchHotel(null, null);
              if (empty($hotelDetail)) {
                  return $hotelDetail;
              }
              // 处理数据
              foreach ($hotelDetail as $key => &$hotelList) {
                  // 过滤没有名称的酒店
                 if (empty($hotelList['name'])) {
                      unset($hotelDetail[$key]);
                      continue;
                  }
                  $searchHotel = array(
                    'hotel' => $hotelList['hotelentity_id'],
                    'hotelUrl' => "http://".MEIZHU_BOOK_HOST."/Home/BookPage/index?hotel={$hotelList['hotelentity_id']}",
                    'address' => $hotelList['address'],
                    'name' => $hotelList['name'],
                  );

                array_push($searchHotelList, $searchHotel);
              }
              unset($hotelList);

              return array_values($searchHotelList);
        }

        if (!is_null($city)) {
            $hotelDetail = $hotelDetailModel->searchHotel($city, $keyWord);
            if (empty($hotelDetail)) {
                return $hotelDetail;
            }
            // 处理数据
            foreach ($hotelDetail as $key => &$hotelList) {
              // 过滤没有名称的酒店
              if (empty($hotelList['name'])) {
                    unset($hotelDetail[$key]);
                    continue;
              }
              $searchHotel = array(
                  'hotel' => $hotelList['hotelentity_id'],
                  'hotelUrl' => "http://".MEIZHU_BOOK_HOST."/Home/BookPage/index?hotel={$hotelList['hotelentity_id']}",
                  'address' => $hotelList['address'],
                  'name' => $hotelList['name'],
              );

              array_push($searchHotelList, $searchHotel);
            }
            unset($hotelList);

            return array_values($searchHotelList);
        }

        // 没有城市 只按关键字搜索
        $hotelDetail = $hotelDetailModel->searchHotel(null, $keyWord);
        if (empty($hotelDetail)) {
            return $hotelDetail;
        }

        // 处理数据
        foreach ($hotelDetail as $key => &$hotelList) {
          // 过滤没有名称的酒店
          if (empty($hotelList['name'])) {
              unset($hotelDetail[$key]);
              continue;
          }
          $searchHotel = array(
              'hotel' => $hotelList['hotelentity_id'],
              'hotelUrl' => "http://".MEIZHU_BOOK_HOST."/Home/BookPage/index?hotel={$hotelList['hotelentity_id']}",
              'address' => $hotelList['address'],
              'name' => $hotelList['name'],
          );

          array_push($searchHotelList, $searchHotel);
        }
        unset($hotelList);

        return array_values($searchHotelList);
    }

    /**
     * 订客栈 搜索符合条件数据
     * @modify wenhui 2016-12-28 创建
     * @modify wenhui 2017-02-22 返回市所在省的客栈 v1.0.02版本判断
     * @param  integer   $cityId     城市id
     * @param  integer   $longtitude     经度
     * @param  integer   $latitude     纬度
     * @param  string   $startDate     开始时间
     * @param  string   $endDate     结束时间
     * @param  string   $version     版本号
     * @param  string   $langSet     语言
     */
    public function bookHotel($city, $longtitude, $latitude, $version, $startDate, $endDate, $langSet) {
        // 获取客栈
        $hotelEntityModel = D('HotelEntity');
        $hotelDetailModel = D('HotelDetail');
        $districtModel = D('District');

        // 没有城市的查询 则获取所有微预定客栈信息 作为推荐客栈 即 isRecommend = 1
        if (is_null($city) || empty($city)) {
            //返回所有没有禁用的客栈id
            $hotelEntity = $hotelEntityModel->getAllIdInfo();
            $hotelEntity = getArrayValueAsIndex($hotelEntity, 'id');
            $hotelIds = array_column($hotelEntity , 'id');
            // 根据客栈ids 获取客栈详情 (即已经开通微预定的客栈)
            $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
            if (empty($hotelinfo)) {
                return array();
            }
            // 作为推荐客栈
            foreach ($hotelinfo as $key => &$hotelList) {
                 $hotelList['isRecommend'] = ENABLE;
            }
            unset($hotelList);
        }

        // 有城市 和经纬度的查询
        if (!is_null($city) && !empty($city) && !empty($longtitude) && !empty($latitude)) {
            $cityModel = D('City');
            if ($version >= '1.0.02') {
                // 获取城市 所在的省微预定客栈数据
                $cityInfo = $cityModel->getInfo($city);
                $provinceId = $cityInfo[0]['province_id'];
                $provinceInfo = $cityModel->getInfoByProvince($provinceId);
                $city = array_column($provinceInfo, 'id');
            }

            //返回所有没有禁用的客栈id
            $hotelEntity = $hotelEntityModel->getInfoByCity($city);
            $hotelEntity = getArrayValueAsIndex($hotelEntity, 'id');
            $hotelIds = array_column($hotelEntity , 'id');
            // 根据客栈ids 获取客栈详情 (即已经开通微预定的客栈)
            $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
            // 不作为推荐客栈 isRecommend = 0
            if (!empty($hotelinfo)) {
                $roomTypeBookLogic = A('Common/RoomTypeBook', 'Logic');
                foreach ($hotelinfo as $key => &$hotelList) {
                      $roomTypeInfo = $roomTypeBookLogic->getRealtimeStatus($hotelList['hotelentity_id'], $startDate, $endDate);
                      $hotelList['isRecommend'] = DISABLE;
                      if (empty($roomTypeInfo)) {
                          unset($hotelinfo[$key]);
                      }
                }
                unset($hotelList);
            }

            // 如果$hotelinfo为空 则获取所有微预定客栈信息 作为推荐客栈
            if (empty($hotelinfo)) {
                //返回所有没有禁用的客栈id
                $hotelEntity = $hotelEntityModel->getAllIdInfo();
                $hotelEntity = getArrayValueAsIndex($hotelEntity, 'id');
                $hotelIds = array_column($hotelEntity , 'id');
                // 根据客栈ids 获取客栈详情 (即已经开通微预定的客栈)
                $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
                if (empty($hotelinfo)) {
                    return array();
                }
                // 作为推荐客栈
                foreach ($hotelinfo as $key => &$hotelList) {
                     $hotelList['isRecommend'] = ENABLE;
                }
                unset($hotelList);
            }
        }

        foreach ($hotelinfo as $key => &$hotelList) {
            // 计算用户经纬度与客栈经纬度 两点的距离
            if (!empty($hotelList['longtitude']) && $hotelList['latitude'] != 0.000000) {
                $distance = getDistance($latitude, $longtitude, $hotelList['latitude'], $hotelList['longtitude']);
                $hotelList['distance'] = $distance;
            } else {
                $hotelList['distance'] = 99999999;
            }

            //  根据地区区号 查询客栈地区区名
            if (isset($hotelEntity[$hotelList['hotelentity_id']])) {
                $districtInfo = $districtModel->getInfo($hotelEntity[$hotelList['hotelentity_id']]['district_id']);
                switch ($langSet) {
                  case 'zh_tw':
                      $hotelList['district'] = explode('/', $districtInfo['name'])[2];
                    break;
                  case 'en_us':
                      $hotelList['district'] = explode('/', $districtInfo['name'])[1];
                    break;
                  default:
                    $hotelList['district'] = explode('/', $districtInfo['name'])[0];
                    break;
                }
            }
        }
        unset($hotelList);

        return array('hotelEntity' => $hotelEntity , 'hotelInfo' => $hotelinfo);
    }

    /**
     * 获取多套个性推荐信息
     * @modify wenhui 2017-01-06 创建
     * @modify wenhui 2017-02-27 增加版本判断
     * @modify wenhui 2017-02-22 返回市所在省的客栈 v1.0.02版本判断
     * @param  integer   $hotelId   客栈ID
     * @return $hotelSpecialDetail     执行结果
     * @param  integer   $longtitude     经度
     * @param  integer   $latitude     纬度
     * @param  integer   $pageIndex     当前页码
     * @param  integer   $pageSize     每页的记录数
     * @param  string   $version     版本号
     */
    public function getManagerInfo($city, $longtitude, $latitude, $pageIndex, $pageSize, $version) {
        // 获取客栈
        $hotelEntityModel = D('HotelEntity');
        $hotelDetailModel = D('HotelDetail');
        $hotelSpecialModel = D('HotelSpecial');

        // 没有城市的查询 则获取所有微预定客栈信息 作为推荐客栈 即 isRecommend = 1
        if (is_null($city) || empty($city)) {
            //返回所有没有禁用的客栈id
            $hotelEntity = $hotelEntityModel->getAllIdInfo();
            $hotelEntity = getArrayValueAsIndex($hotelEntity, 'id');
            $hotelIds = array_column($hotelEntity , 'id');
            // 根据客栈ids 获取客栈详情 (即已经开通微预定的客栈)
            $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
            if (empty($hotelinfo)) {
                return array();
            }
            // 作为推荐客栈
            foreach ($hotelinfo as $key => &$hotelList) {
                // 获取掌柜说信息
                $hotelSpecialDetail = $hotelSpecialModel->getSpecials($hotelList['hotelentity_id']);
                // 过滤无掌柜说图片的客栈
                if (empty($hotelSpecialDetail['specialimageurl'])) {
                    unset($hotelinfo[$key]);
                    continue;
                }
                $hotelList['isRecommend'] = ENABLE;
            }
            unset($hotelList);
        }

        // 有城市 和经纬度的查询
        if (!is_null($city) && !empty($city) && !empty($longtitude) && !empty($latitude)) {
            $cityModel = D('City');
            if ($version >= '1.0.02') {
              // 获取城市 所在的省微预定客栈数据
              $cityInfo = $cityModel->getInfo($city);
              $provinceId = $cityInfo[0]['province_id'];
              $provinceInfo = $cityModel->getInfoByProvince($provinceId);
              $city = array_column($provinceInfo, 'id');
            }

            ///返回所有没有禁用的客栈id
            $hotelEntity = $hotelEntityModel->getInfoByCity($city);
            $hotelEntity = getArrayValueAsIndex($hotelEntity, 'id');
            $hotelIds = array_column($hotelEntity , 'id');
            // 根据客栈ids 获取客栈详情 (即已经开通微预定的客栈)
            $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
            // 不作为推荐客栈
            if (!empty($hotelinfo)) {
                foreach ($hotelinfo as $key => &$hotelList) {
                    // 获取掌柜说信息
                    $hotelSpecialDetail = $hotelSpecialModel->getSpecials($hotelList['hotelentity_id']);
                    // 过滤无掌柜说图片的客栈
                    if (empty($hotelSpecialDetail['specialimageurl'])) {
                        unset($hotelinfo[$key]);
                        continue;
                    }
                    $hotelList['isRecommend'] = DISABLE;
                }
            } else {
                //返回所有没有禁用的客栈id
                $hotelEntity = $hotelEntityModel->getAllIdInfo();
                $hotelEntity = getArrayValueAsIndex($hotelEntity, 'id');
                $hotelIds = array_column($hotelEntity , 'id');
                // 根据客栈ids 获取客栈详情 (即已经开通微预定的客栈)
                $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
                if (empty($hotelinfo)) {
                    return array();
                }
                // 作为推荐客栈
                foreach ($hotelinfo as $key => &$hotelList) {
                      // 获取掌柜说信息
                      $hotelSpecialDetail = $hotelSpecialModel->getSpecials($hotelList['hotelentity_id']);
                      // 过滤无掌柜说图片的客栈
                      if (empty($hotelSpecialDetail['specialimageurl'])) {
                          unset($hotelinfo[$key]);
                          continue;
                      }
                      $hotelList['isRecommend'] = ENABLE;
                }
                unset($hotelList);
            }
        }

        // 保存客栈掌柜说数组
        $saveHotelManager = array();
        // 存放美住线上测试客栈  并排序到最后
        $testHotelManager = array();
        foreach ($hotelinfo as $key => &$hotelList) {
            $hotelSpecialDetail = $hotelSpecialModel->getSpecials($hotelList['hotelentity_id']);
            // 计算用户经纬度与客栈经纬度 两点的距离
            if (!empty($hotelList['longtitude']) && $hotelList['latitude'] != 0.000000) {
                $distance = getDistance($latitude, $longtitude, $hotelList['latitude'], $hotelList['longtitude']);
                $hotelList['distance'] = $distance;
            } else {
                $hotelList['distance'] = 99999999;
            }
            $hotelManagerList = array(
                        'hotel' => $hotelList['hotelentity_id'],
                        'hotelName' => $hotelList['name'],
                        'distance' => $hotelList['distance'],
                        'isRecommend' => $hotelList['isRecommend'],
                        'click' => $hotelList['click'],
                        'specialImageUrl' => isset($hotelSpecialDetail['specialimageurl']) && !empty($hotelSpecialDetail['specialimageurl']) ? MEIZHU_URL . $hotelSpecialDetail['specialimageurl'] : '',
                        'specialDescription' => isset($hotelSpecialDetail['special']) ? $hotelSpecialDetail['special'] : '',
                        'isEnable' => 0,
            );
            // v1.0.03以上 增加分享链接
            if ($version >= '1.0.03') {
                 $hotelManagerList['managerShareLink'] = "http://" . MEIZHU_BOOK_HOST . "/Home/BookPage/shopkeeper?hotel={$hotelList['hotelentity_id']}&specialId={$hotelSpecialDetail['id']}";
            }
            // v1.0.02 添加客栈最低价
            if ($version >= '1.0.02') {
              // 获取客栈最低价
              $hotelRoomInfo = getHoteRoomTypePrice($hotelList['hotelentity_id']);
              if (isset($hotelRoomInfo['minPrice'])) {
                  $hotelManagerList['minPrice'] = $hotelRoomInfo['minPrice'];
              }
            }

            // 取出测试客
            if ($hotelEntity[$hotelList['hotelentity_id']]['usage'] == 3) {
                array_push($testHotelManager, $hotelManagerList);
            } else {
                array_push($saveHotelManager, $hotelManagerList);
            }
        }
        unset($hotelList);

        /**
         *  排序
         *  排序方式 推荐客栈按点击量  有城市搜索 按最近距离
         *  SORT_ASC - 默认，按升序排列。(A-Z)
         *  SORT_DESC - 按降序排列。(Z-A)
         *  随后可以指定排序的类型：
         *  SORT_REGULAR - 默认。将每一项按常规顺序排列。
         *  SORT_NUMERIC - 将每一项按数字顺序排列。
         *  SORT_STRING - 将每一项按字母顺序排列
         */
        $saveHotelManager = array_values($saveHotelManager);
        if ($saveHotelManager[0]['isRecommend'] == 1) {
            $saveHotelManager = my_sort($saveHotelManager, 'click', SORT_DESC, SORT_NUMERIC);
        } else {
            usort($saveHotelManager, function($left, $right) {
              return $left['distance'] - $right['distance'];
            });
        }
        //美住线上测试客栈排序到最后
        $saveHotelManager = array_merge($saveHotelManager, $testHotelManager);

        // 分页
        $saveHotelManager = generatePageInfo($saveHotelManager, $pageIndex, $pageSize);

        return $saveHotelManager;
    }

    /**
     * 根据$hotelId获取多套个性推荐信息
     * @modify wenhui 2017-01-06 创建
     * @modify wenhui 2017-02-27 增加版本判断
     * @param int $hotelId 客栈id
     * @param string $version 版本号
     * @return array              执行结果
     */
    public function getInfoByHotel($hotelId, $version) {
        $hotelSpecialModel = D('HotelSpecial');
        // 获取多套个性推荐信息
        $hotelSpecialDetail = $hotelSpecialModel->getInfoByHotel($hotelId);
        // 组装返回数据格式
        $data['specialInfo'] = array();

        if (empty($hotelSpecialDetail)) {
            $special = array(
                'specialId' => '',
                'specialDescription' => '',
                'specialImageUrl' => '',
            );

            array_push($data['specialInfo'], $special);
            $data['bookUrl'] = "http://" . MEIZHU_BOOK_HOST . "/Home/BookPage/index?hotel={$hotelId}&name=Bind";

            return $data;
        }

        foreach ($hotelSpecialDetail as $key => $specialList) {
             $special = array(
                'specialId' => $specialList['id'],
                'specialDescription' => $specialList['special'],
                'index' => $specialList['sortindex'],
             );

             if (isset($specialList['specialimageurl']) && !empty($specialList['specialimageurl'])) {
                  $special['specialImageUrl'] = MEIZHU_URL . $specialList['specialimageurl'];
             } else {
                 $special['specialImageUrl'] = '';
             }

             if($version >= '1.0.03'){
                $special['managerShareLink'] = "http://" . MEIZHU_BOOK_HOST . "/Home/BookPage/shopkeeper?hotel={$hotelId}&specialId={$specialList['id']}";
                $special['updateTime'] = strtotime($specialList['updatetime']);
                $special['isActive'] = $specialList['isactive'];
             }

             array_push($data['specialInfo'], $special);
        }
        unset($specialList);

        // 微预定订房房型列表 url
        $data['bookUrl'] = "http://" . MEIZHU_BOOK_HOST . "/Home/BookPage/index?hotel={$hotelId}&name=Bind";

        return empty($data) ? array() : $data;
    }

    /**
     * 获取优选品牌
     * @modify wenhui 2017-01-06 创建
     * @modify wenhui 2017-02-22 返回市所在省的客栈 v1.0.02版本判断
     * @param  integer   $city   城市区号
     * @return $hotelSpecialDetail     执行结果
     * @param  integer   $longtitude     经度
     * @param  integer   $latitude     纬度
     * @param  integer   $pageIndex     当前页码
     * @param  integer   $pageSize     每页的记录数
     * @param  sting   $version     版本号
     * @param  sting   $langSet     语言
     */
    public function getBrandInfo($city, $longtitude, $latitude, $pageIndex, $pageSize, $version, $langSet) {
        // 获取客栈
        $hotelEntityModel = D('HotelEntity');
        $hotelDetailModel = D('HotelDetail');
        $districtModel = D('District');
        $hotelRoomTypePriceModel = D('HotelRoomTypePrice');
        $roomTypePriceModel = D('RoomTypePrice');
        $roomTypeDetailModel = D('RoomTypeDetail');
        $roomTypeModel = D('RoomType');
        $roomInfoModel = D('RoomInfo');

        // 有城市 和经纬度的查询
        if (!is_null($city) && !empty($city) && !empty($longtitude) && !empty($latitude)) {
            $cityModel = D('City');
            if ($version >= '1.0.02') {
                // 获取城市 所在的省微预定客栈数据
                $cityInfo = $cityModel->getInfo($city);
                $provinceId = $cityInfo[0]['province_id'];
                $provinceInfo = $cityModel->getInfoByProvince($provinceId);
                $city = array_column($provinceInfo, 'id');
            }

            ///返回所有没有禁用的客栈id
            $hotelEntity = $hotelEntityModel->getInfoByCity($city);
            $hotelEntity = getArrayValueAsIndex($hotelEntity, 'id');
            $hotelIds = array_column($hotelEntity , 'id');
            // 根据客栈ids 获取客栈详情 (即已经开通微预定的客栈)
            $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
            // 不作为推荐客栈
            if (!empty($hotelinfo)) {
                foreach ($hotelinfo as $key => &$hotelList) {
                      // 过滤没有名称 和 没有封面图片 的酒店
                      if (empty($hotelList['name']) || empty($hotelList['coverimageurl'])) {
                          unset($hotelinfo[$key]);
                          continue;
                      }
                      $hotelList['isRecommend'] = DISABLE;
                }
                unset($hotelList);
            }

            // 如果$hotelinfo为空 则获取所有微预定客栈信息 作为推荐客栈
            if (empty($hotelinfo)) {
                //返回所有没有禁用的客栈id
                $hotelEntity = $hotelEntityModel->getAllIdInfo();
                $hotelEntity = getArrayValueAsIndex($hotelEntity, 'id');
                $hotelIds = array_column($hotelEntity , 'id');
                // 根据客栈ids 获取客栈详情 (即已经开通微预定的客栈)
                $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
                if (empty($hotelinfo)) {
                    return array();
                }
                // 作为推荐客栈
                foreach ($hotelinfo as $key => &$hotelList) {
                      // 过滤没有名称 和 没有封面图片 的酒店
                      if (empty($hotelList['name']) || empty($hotelList['coverimageurl'])) {
                          unset($hotelinfo[$key]);
                          continue;
                      }
                      $hotelList['isRecommend'] = ENABLE;
                }
                unset($hotelList);
            }
        } else {
          //返回所有没有禁用的客栈id
          $hotelEntity = $hotelEntityModel->getAllIdInfo();
          $hotelEntity = getArrayValueAsIndex($hotelEntity, 'id');
          $hotelIds = array_column($hotelEntity , 'id');
          // 根据客栈ids 获取客栈详情 (即已经开通微预定的客栈)
          $hotelinfo = $hotelDetailModel->getInfoByHotels($hotelIds);
          if (empty($hotelinfo)) {
              return array();
          }
          // 作为推荐客栈
          foreach ($hotelinfo as $key => &$hotelList) {
                // 过滤没有名称 和 没有封面图片 的酒店
                if (empty($hotelList['name']) || empty($hotelList['coverimageurl'])) {
                    unset($hotelinfo[$key]);
                    continue;
                }
                $hotelList['isRecommend'] = ENABLE;
          }
          unset($hotelList);
        }

        // 存放美住线上测试客栈  并排序到最后
        $testHotel = array();
        // 其他客栈
        $allHotelinfo = array();
        // 处理数据
        foreach ($hotelinfo as $key => &$hotelList) {
            // 计算用户经纬度与客栈经纬度 两点的距离
            if (!empty($hotelList['longtitude']) && $hotelList['latitude'] != 0.000000) {
                $distance = getDistance($latitude, $longtitude, $hotelList['latitude'], $hotelList['longtitude']);
                $hotelList['distance'] = $distance;
            } else {
                $hotelList['distance'] = 99999999;
            }

            //  根据地区区号 查询客栈地区区名  或  根据已经开通微预定的客栈ids 查询客栈地区区名
            if (isset($hotelEntity[$hotelList['hotelentity_id']])) {
                $districtInfo = $districtModel->getInfo($hotelEntity[$hotelList['hotelentity_id']]['district_id']);
                switch ($langSet) {
                  case 'zh-cn':
                      $hotelList['district'] = explode('/', $districtInfo['name'])[0];
                    break;
                  case 'en-us':
                      $hotelList['district'] = explode('/', $districtInfo['name'])[1];
                    break;
                  default:
                      $hotelList['district'] = explode('/', $districtInfo['name'])[2];
                    break;
                }
            }

            $hotelList['hotel'] = $hotelList['hotelentity_id'];
            $hotelList['hotelName'] = $hotelList['name'];
            $hotelList['bookUrl'] = "http://".MEIZHU_BOOK_HOST."/Home/BookPage/index?hotel={$hotelList['hotelentity_id']}&name=Bind";
            $hotelList['isEnable'] = 0;
            if (isset($hotelList['coverimageurl']) && !empty($hotelList['coverimageurl'])) {
                 $hotelList['coverUrl'] = MEIZHU_URL . $hotelList['coverimageurl'];
            } else {
                 $hotelList['coverUrl'] = '';
            }

            // 获取客栈房型的最高价与最低价 获取每个客栈房间总数(即 第二个参数为true)
            $hotelRoomInfo = getHoteRoomTypePrice($hotelList['hotelentity_id'], true);
            $hotelList['roomTotal'] = $hotelRoomInfo['roomTotal'];

            if (isset($hotelRoomInfo['minPrice'])) {
                $hotelList['maxPrice'] = $hotelRoomInfo['maxPrice'];
                $hotelList['minPrice'] = $hotelRoomInfo['minPrice'];
            }

            // 根据客栈id获取所有房型id
            // 获取客栈所有上架房型
            $roomTypeDetail = $roomTypeDetailModel->getInfoByHotel($hotelList['hotelentity_id'], true);
            $roomTypeId = array_column($roomTypeDetail, 'roomtype_id');
            // 根据客栈id 获取每个客栈房间总数
            if (!empty($roomTypeDetail)) {
                $roomInfo = $roomInfoModel->getInfoByRoomType($hotelList['hotelentity_id'], $roomTypeId);
                $hotelList['roomTotal'] = count($roomInfo);
            } else {
                $hotelList['roomTotal'] = 0;
            }

            // 获取客栈房型当天价格日历的价格
            if (!empty($roomTypeDetail)) {
                foreach ($roomTypeDetail as $index => &$roomTypeDetailItem) {
                    // 房型折扣值
                    $roomTypeDiscountInfo = $roomTypeModel->getInfo($roomTypeDetailItem['roomtype_id']);
                    $discount = ($roomTypeDiscountInfo['discount'])/10;

                    // 获取预定价格
                    $hotelRoomTypePriceList = $hotelRoomTypePriceModel->getBookPrice($roomTypeDetailItem['roomtype_id'], $startDate);
                    $roomTypePriceList = $roomTypePriceModel->getBookPrice($roomTypeDetailItem['roomtype_id'], $startDate);
                    if (!empty($hotelRoomTypePriceList)) {
                        $roomTypeDetailItem['price'] = number_format($hotelRoomTypePriceList[0]['price'],2, '.', '');
                    } else {

                        if (!empty($roomTypePriceList)) {
                            $roomTypeDetailItem['price'] = number_format((($roomTypePriceList[0]['price']*1000)/1000) * $discount ,2, '.', '');
                        } else {
                            $roomTypeDetailItem['price'] = number_format((($roomTypeDiscountInfo['price']*1000)/1000) * $discount, 2, '.', '');
                        }
                    }
                }
                unset($roomTypeDetailItem);
            }

            if (!empty($roomTypeDetail)) {
                // 获取客栈房型的最高价与最低价
                $roomTypeDetail = my_sort($roomTypeDetail, 'price', SORT_DESC, SORT_NUMERIC);
                $hotelList['maxPrice'] = $roomTypeDetail[0]['price'];
                $hotelList['minPrice'] = $roomTypeDetail[count($roomTypeDetail) - 1]['price'];
            }

            unset($hotelList['coverimageurl']);
            unset($hotelList['id']);
            unset($hotelList['description']);
            unset($hotelList['longtitude']);
            unset($hotelList['latitude']);
            unset($hotelList['ordertimeout']);
            unset($hotelList['createtime']);
            unset($hotelList['updatetime']);
            unset($hotelList['creator']);
            unset($hotelList['operator']);
            unset($hotelList['isdelete']);
            unset($hotelList['special']);
            unset($hotelList['specialimageurl']);
            unset($hotelList['telephone']);
            unset($hotelList['wechat']);
            unset($hotelList['hotline']);
            unset($hotelList['mobile']);
            unset($hotelList['contact']);
            unset($hotelList['ordertimeout']);
            unset($hotelList['enable']);
            unset($hotelList['special']);
            unset($hotelList['address']);
            unset($hotelList['name']);

            // 取出测试客
            if ($hotelEntity[$hotelList['hotelentity_id']]['usage'] == 3) {
                unset($hotelList['hotelentity_id']);
                array_push($testHotel, $hotelList);
            } else {
                unset($hotelList['hotelentity_id']);
                array_push($allHotelinfo, $hotelList);
            }
        }
        unset($hotelList);

        /**
         *  排序
         *  排序方式 推荐客栈按点击量  有城市搜索 按最近距离
         *  SORT_ASC - 默认，按升序排列。(A-Z)
         *  SORT_DESC - 按降序排列。(Z-A)
         *  随后可以指定排序的类型：
         *  SORT_REGULAR - 默认。将每一项按常规顺序排列。
         *  SORT_NUMERIC - 将每一项按数字顺序排列。
         *  SORT_STRING - 将每一项按字母顺序排列
         */
        if ($allHotelinfo[0]['isRecommend'] == 1) {
            $allHotelinfo = my_sort($allHotelinfo, 'click', SORT_DESC, SORT_NUMERIC);
        } else {
            usort($allHotelinfo, function($left, $right) {
              return $left['distance'] - $right['distance'];
            });
        }

        //美住线上测试客栈排序到最后
        $allHotelinfo = array_merge($allHotelinfo, $testHotel);
        // 分页
        $allHotelinfo = generatePageInfo($allHotelinfo, $pageIndex, $pageSize);

        return $allHotelinfo;
    }

    /**
     * 记录点击量
     * @modify wenhui 2017-01-06    创建
     */
    public function rememberClick($hotel) {
        $hotelDetailModel = D('HotelDetail');
        $hotelDetailModel->saveClick($hotel);
    }
}